(ns fxbattle.server.ai
 (:require fxbattle.server.core)
 (:gen-class))

(def running (atom true))

;;; Add a random path - testing
(defn- add-random-path
  "Adds a random path to the game"
  [player game]
  (let [orig-x (rand-int (:height game))
        orig-y (rand-int (:width game))
        dest-x (+ orig-x (rand-int 3) -1)
        dest-y (+ orig-y (rand-int 3) -1)]
    (fxbattle.server.core/set-path orig-x orig-y dest-x dest-y true player game)))

(defn- computer-player
  "Runs the AI player"
  [player game]
  (while @running
    (add-random-path player game)
    (. Thread (sleep 250))))

(defn run-computer-player
  "Starts a thread running the computer player"
  [name game]
  (reset! running true)
  (let [player (fxbattle.server.core/add-player name game)]
    (.start (Thread. (fn [] (computer-player player game))))))

(defn stop-computer-players
  "Stops the thread running the computer player"
  []
  (reset! running false))